package cn.edu.sanxiau.www.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.sanxiau.www.model.Permission;
import cn.edu.sanxiau.www.model.Role;
import cn.edu.sanxiau.www.model.RolePermission;
import cn.edu.sanxiau.www.model.User;
import cn.edu.sanxiau.www.model.UserRole;
import frame.controller.BaseController2;

@WebServlet(urlPatterns = "/permissionController.do")
public class PermissionController extends BaseController2 {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    //列表
    public String listPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        //3.调用业务；
        List<Permission> permissionList_db = permissionService.queryAllPermission();
//		for (Permission permission_db : permissionList_db) {
//			System.out.println("--------listpermission--------"+permission_db);
//		}		
        //响应这个数据
        req.setAttribute("permissionList_db", permissionList_db);
        //2.跳转页面；
        return "view/system/permission/permissionList.jsp";

    }

    //列表
    public String addPermissionUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
//		System.out.println("--------addPermissionUI--------");
        //3.调用业务；
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        //响应这个数据
        req.setAttribute("permissionList_db", permissionList_db);
        System.out.println("--------addPermissionUI--------" + permissionList_db);
        //2.跳转页面；
        return "view/system/permission/addPermission.jsp";
    }

    // 增加
    public String addPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        //a.父id
        String pIdSTR = req.getParameter("pIdSTR");
        //字符类型转数字类型
        int p_id = Integer.parseInt(pIdSTR);

        //b.表单数据
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String url = req.getParameter("url");
        String permission_code = req.getParameter("permission_code");
        String state = req.getParameter("state");
        String icon = req.getParameter("icon");

        //封装
        Permission permission = new Permission();
        permission.setP_id(p_id);
        permission.setName(name);
        permission.setType(type);
        permission.setUrl(url);
        permission.setPermission_code(permission_code);
        permission.setState(state);
        permission.setIcon(icon);

        System.out.println("-----permissionController---addpermission--------" + permission);

        //3.调用业务；
        int n = permissionService.addPermissionByPermission(permission);

        //2.跳转页面；
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        //响应这个数据
        req.setAttribute("permissionList_db", permissionList_db);
        //2.跳转页面；
        return "view/system/permission/permissionList.jsp";
    }


    //删除
    public String deletePermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String permission_idStr = req.getParameter("permission_idStr");
        //字符转数字类型
        int permission_id = Integer.parseInt(permission_idStr);
        System.out.println("--------permission_id--------" + permission_id);

        //3.调用业务；
        int n = permissionService.deletePermissionByPermission_id(permission_id);


        //2.跳转页面；
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        //响应这个数据
        req.setAttribute("permissionList_db", permissionList_db);
        //2.跳转页面；
        return "view/system/permission/permissionList.jsp";

    }


    //修改UI
    public String updateUIPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String permission_idStr = req.getParameter("permission_idStr");
        //字符转数字类型
        int permission_id = Integer.parseInt(permission_idStr);
        System.err.println("========" + permission_id);

        //3.调用业务；
        //a.准备所有父级数据
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        req.setAttribute("permissionList_db", permissionList_db);

        //b.被修改数据
        Permission permission_db = permissionService.queryPermissionByPermission_id(permission_id);
        req.setAttribute("permission", permission_db);


        //2.跳转页面；
        return "view/system/permission/updatePermission.jsp";

    }


    //修改
    public String updatePermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；

        String permission_idStr = req.getParameter("permission_idStr");
        //字符转数字类型
        int permission_id = Integer.parseInt(permission_idStr);

        //a.父id
        String pIdSTR = req.getParameter("pIdSTR");
        //字符类型转数字类型
        int p_id = Integer.parseInt(pIdSTR);


        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String url = req.getParameter("url");
        String permission_code = req.getParameter("permission_code");
        String state = req.getParameter("state");
        String icon = req.getParameter("icon");


        //封装
        Permission permission = new Permission();
        permission.setPermission_id(permission_id);
        permission.setP_id(p_id);
        permission.setName(name);
        permission.setType(type);
        permission.setUrl(url);
        permission.setPermission_code(permission_code);
        permission.setState(state);
        permission.setIcon(icon);


        System.err.println("----------666--------updatepermission-----9999-------" + permission);


        //3.调用业务；
        int n = permissionService.updatePermissionByPermission(permission);

        //2.跳转页面；
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        //响应这个数据
        req.setAttribute("permissionList_db", permissionList_db);
        //2.跳转页面；
        return "view/system/permission/permissionList.jsp";

    }


    //分配权限UI
    public String FPPermissionUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String role_idStr = req.getParameter("role_idStr");
        //字符转数字类型
        int role_id = Integer.parseInt(role_idStr);
        System.err.println("------------------FPRoleUI()------------" + role_id);

        //3.调用业务；
        //a.给那个角色分配
        Role role_db = roleService.queryRoleByRole_id(role_id);
        req.setAttribute("role", role_db);

        //b.所有权限数据
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        req.setAttribute("permissionList_db", permissionList_db);

        //d.选中权限集合 (XZ:选中)
        List<Permission> XZPermissionList_db = permissionService.queryXZPermissionByRole_id(role_id);
//	        for (Permission permission : XZPermissionList_db) {
//	        	System.err.println(permission);
//	        }

        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission : XZPermissionList_db) {
            list.add(permission.getPermission_id());
        }
        req.setAttribute("ids", list);


        //2.跳转页面；
        return "view/system/permission/FPPermission.jsp";

    }

    //分配权限
    public String FPPermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        //a.角色
        String role_idStr = req.getParameter("role_idStr");
        //字符类型转数字类型
        int role_id = Integer.parseInt(role_idStr);
//			System.err.println("---------------"+role_id);
        //b.权限
        String[] permission_idSTRS = req.getParameterValues("permission_idSTRS");

        //先删除（判断数据库里面有没有？条件：role_id）
        int m = rolePermissionService.deleteRolePermissionByRole_id(role_id);

        for (String permission_idSTR : permission_idSTRS) {
//				System.err.println("================"+permission_idSTR);
            //字符类型转数字类型
            int permission_id = Integer.parseInt(permission_idSTR);

            //3.调用业务；
            //封装
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRole_id(role_id);
            rolePermission.setPermission_id(permission_id);

            //业务增加
            int n = rolePermissionService.addRolePermissionByRolePermission(rolePermission);
        }
        //2.跳转页面；
        //a.给那个角色分配
        Role role_db = roleService.queryRoleByRole_id(role_id);
        req.setAttribute("role", role_db);

        //b.所有权限数据
        List<Permission> permissionList_db = permissionService.queryAllPermission();
        req.setAttribute("permissionList_db", permissionList_db);

        //c.友情提示
        req.setAttribute("message", "分配权限成功！继续分配吗？");

        //d.选中权限集合 (XZ:选中)
        List<Permission> XZPermissionList_db = permissionService.queryXZPermissionByRole_id(role_id);
//	        for (Permission permission : XZPermissionList_db) {
//	        	System.err.println(permission);
//	        }

        List<Integer> list = new ArrayList<Integer>();
        for (Permission permission : XZPermissionList_db) {
            list.add(permission.getPermission_id());
        }
        req.setAttribute("ids", list);

        return "view/system/permission/FPPermission.jsp";

    }

}
